//
//  ParkingPortMapViewController.m
//  KuWeiPark
//
//  Created by Kevin Su on 15/5/27.
//  Copyright (c) 2015年 Clief. All rights reserved.
//

#import "ParkingPortMapViewController.h"
#import "AppMacro.h"
#import "ParkingSpaceTipView.h"
#import "HttpClientManager.h"

@interface ParkingPortMapViewController ()

@end

@implementation ParkingPortMapViewController{
    
    ParkingSpaceTipView *tipView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    
    [self initMap];
    
    [self initTipView];
}

- (void)initTipView{
    
    tipView = [[[NSBundle mainBundle] loadNibNamed:@"ParkingSpaceTipView" owner:self options:nil] lastObject];
    tipView.frame = CGRectMake(0, 0, self.view.frame.size.width, 0);
    [self.view addSubview:tipView];
    [UIView animateWithDuration:0.3 animations:^{
        
        tipView.frame = CGRectMake(0, 0, self.view.frame.size.width, 63);
    } completion:^(BOOL finished) {
        
        for(UIView *view in tipView.subviews){
            
            view.hidden = NO;
        }
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            for(UIView *view in tipView.subviews){
                
                view.hidden = YES;
            }
            [UIView animateWithDuration:0.3 animations:^{
                
                tipView.frame = CGRectMake(0, 0, self.view.frame.size.width, 0);
            } completion:^(BOOL finished) {
                
                
            }];
        });
    }];
    
}

- (void)initMap{
    
    if(self.mkScrollView == nil){
        
        self.mkScrollView = [[MKScrollView alloc] init];
    }
    
    self.mkScrollView = [self.mkScrollView initWithPhoto:self.parkingPortMap Frame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width) Center:CGPointMake([self.parkingPortModel.centerX integerValue], [self.parkingPortModel.centerY integerValue])];
    self.mkScrollView.center = CGPointMake(self.view.frame.size.width / 2.0, (self.view.frame.size.height) / 2.0);
    self.mkScrollView.transform = CGAffineTransformMakeRotation(90 * M_PI / 180.0);
    self.mkScrollView.mkDelegate = self;
    [self.view addSubview:self.mkScrollView];
    
    [self initParkableSpaceWithParkPortModel:self.parkingPortModel];
}

- (void)initParkableSpaceWithParkPortModel:(ParkPortModel *)parkPortModel{
    
    //初始化 描绘  可停车位
    for(ParkingSpaceInfoModel *spaceInfoModel in parkPortModel.freeParkingSpaceList){
        
        //撒点
        [self addButtonToMapviewWithParkSpaceinfoModel:spaceInfoModel WithStatus:ParkingSpaceStatusFree];
    }
    
    if(self.selectedFirstParkingSpaceModel != nil){
        
        //用户已经选中了第一个车位
        [self addButtonToMapviewWithParkSpaceinfoModel:self.selectedFirstParkingSpaceModel WithStatus:ParkingSpaceStatusMeSelected];
    }
    
    if(self.selectedSecondParkingSpaceModel != nil){
        
        //用户已经选中了第二个车位
        [self addButtonToMapviewWithParkSpaceinfoModel:self.selectedSecondParkingSpaceModel WithStatus:ParkingSpaceStatusMeSelected];
    }
}

- (void)addButtonToMapviewWithParkSpaceinfoModel:(ParkingSpaceInfoModel *)spaceInfoModel WithStatus:(int)status{
    
    //往地图上添加按钮
    NSInteger button_width = [spaceInfoModel.rightPointX integerValue] - [spaceInfoModel.leftPointX integerValue];
    NSInteger button_height = [spaceInfoModel.rightPointY integerValue] - [spaceInfoModel.leftPointY integerValue];
    
    ParkingSpaceButton *button = [[ParkingSpaceButton alloc] initWithFrame:CGRectMake([spaceInfoModel.leftPointX integerValue], [spaceInfoModel.leftPointY integerValue], button_width, button_height)];
    button.parkingSpaceModel = spaceInfoModel;
    [button setTitle:spaceInfoModel.spaceNo forState:UIControlStateNormal];
    [button setTitle:spaceInfoModel.spaceNo forState:UIControlStateSelected];
    [button addTarget:self action:@selector(spaceButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
    switch (status) {
        case ParkingSpaceStatusFree:
        {
            button.selected = NO;
            button.backgroundColor = [UIColor greenColor];
            break;
        }
        case ParkingSpaceStatusMeSelected:
        {
            button.selected = YES;
            button.backgroundColor = [UIColor redColor];
            break;
        }
        case ParkingSpaceStatusOtherSelected:
        {
            button.enabled = NO;
            button.backgroundColor = RGBACOLOR(255, 180, 0, 1);
            break;
        }
        default:
            break;
    }
    
    [self.mkScrollView.parkPortImageView addSubview:button];
}

- (void)spaceButtonSelected:(ParkingSpaceButton *)spaceButton{
    
    if(!spaceButton.selected){
        
        //如果改车位没有选过
        //判断 用户是否已经选中过2个车位
        if(self.selectedFirstParkingSpaceModel == nil){
            
            //用户没有选折 第一个车位 则 该次选中的车位 做为第一个车位
            self.selectedFirstParkingSpaceModel = spaceButton.parkingSpaceModel;
            spaceButton.selected = YES;
            spaceButton.backgroundColor = [UIColor redColor];
            
            return;
        }
        
        if(self.selectedSecondParkingSpaceModel == nil){
            
            //用户没有选折 第二个车位 则 该次选中的车位 做为第二个车位
            self.selectedSecondParkingSpaceModel = spaceButton.parkingSpaceModel;
            spaceButton.selected = YES;
            spaceButton.backgroundColor = [UIColor redColor];

            return;
        }
        
        //否则用户两个车位都已经选择 提示错误信息
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"酷位停车提示" message:@"最多选择2个车位" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
    }else{
        
        //当前 车位已经选中 取消选择该车位
        if([spaceButton.parkingSpaceModel isEqual:self.selectedFirstParkingSpaceModel]){
            
            //取消选中第一个车位
            self.selectedFirstParkingSpaceModel = nil;
        }
        
        if([spaceButton.parkingSpaceModel isEqual:self.selectedSecondParkingSpaceModel]){
            
            //取消选中第二个车位
            self.selectedSecondParkingSpaceModel = nil;
        }
        
        spaceButton.selected = NO;
        spaceButton.backgroundColor = [UIColor greenColor];
    }
}

- (void)singleTapDetected:(NSNumber *)pointX Y:(NSNumber *)pointY{
    
    NSLog(@"Touch Map at Point X : %@  Y: %@", pointX, pointY);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IBActions
- (IBAction)back:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)submitAction:(id)sender {
    
    //提交选择 跳转到停车场界面
    NSMutableDictionary *userinfo = [[NSMutableDictionary alloc] init];
    if(self.selectedFirstParkingSpaceModel != nil){
        
        [userinfo setObject:self.selectedFirstParkingSpaceModel forKey:@"FirstParkingSpace"];
    }
    if(self.selectedSecondParkingSpaceModel != nil){
        
        [userinfo setObject:self.selectedSecondParkingSpaceModel forKey:@"SecondParkingSpace"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:Notify_ChooseParkingSpace object:nil userInfo:userinfo];
    [self.navigationController popViewControllerAnimated:YES];
}


@end
